home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / xlib / xswap.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  10KB  |  415 lines

  1. /*
  2.  * (c) Copyright 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include <X11/Xlib.h>
  39. #include <X11/Xutil.h>
  40. #include <GL/glx.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <X11/keysym.h>
  44.  
  45. static int RGBattributes_DB[] = {
  46.     GLX_RGBA,
  47.     GLX_RED_SIZE, 1,
  48.     GLX_GREEN_SIZE, 1,
  49.     GLX_BLUE_SIZE, 1,
  50.     GLX_DOUBLEBUFFER,
  51.     None,
  52. };
  53.  
  54. static int CIattributes_DB[] = {
  55.     GLX_DOUBLEBUFFER,
  56.     None,
  57. };
  58.  
  59. enum {
  60.     BLACK = 0,
  61.     RED,
  62.     GREEN,
  63.     YELLOW,
  64.     BLUE,
  65.     MAGENTA,
  66.     CYAN,
  67.     WHITE
  68. };
  69.  
  70. #define COLOR_OFFSET_1    16
  71. #define COLOR_OFFSET_2    32
  72.  
  73.  
  74. static float rgbMap[][3] = {
  75.     {0, 0, 0},
  76.     {1, 0, 0},
  77.     {0, 1, 0},
  78.     {1, 1, 0},
  79.     {0, 0, 1},
  80.     {1, 0, 1},
  81.     {0, 1, 1},
  82.     {1, 1, 1}
  83. };
  84.  
  85. static long W = 300, H = 300;
  86. static Display *dpy;
  87. static Window window;
  88. static Colormap cmap;
  89. int drawX = 1;
  90. int drawGL = 1;
  91. int doubleBuf = 1;
  92. int swaps = 1;
  93. GLenum drawBuffer = GL_FRONT;
  94. static long rgb;
  95. static long ci1 = BLUE, ci2 = GREEN;
  96.  
  97.  
  98.  
  99. static GLuint LoadGLFont(void)
  100. {
  101.     XFontStruct *fontInfo;
  102.     Font id;
  103.     GLuint base;
  104.     int first, last;
  105.  
  106.     static char pattern1[] = "-adobe-courier-bold-o-normal--18-*-*-*-*-*-*-*";
  107.  
  108.     fontInfo = XLoadQueryFont(dpy, pattern1);
  109.  
  110.     id = fontInfo->fid;
  111.     first = (int)fontInfo->min_char_or_byte2;
  112.     last = (int)fontInfo->max_char_or_byte2;
  113.  
  114.  
  115.     base = glGenLists(last+1);
  116.     if (base == 0) {
  117.         return 0;
  118.     }
  119.     glXUseXFont(id, first, last-first+1, (int)(base+first));
  120.     return base;
  121. }
  122.  
  123.  
  124. static void Usage(void)
  125. {
  126.     printf("Usage: xswap [-c]\n");
  127.     printf("   -c:  Run in color index mode\n");
  128.     exit(-1);
  129. }
  130.  
  131. static GLint DrawXFont(GLint x, GLint y, char *string, GLint len)
  132. {
  133.     static char pattern1[] = "-adobe-courier-bold-o-normal--18-*-*-*-*-*-*-*";
  134.     static char pattern2[] = "-*-*-*-*-*-*-18-*-*-*-*-*-*-*";
  135.     static char size[] = "456781";
  136.     GC gc;
  137.     XGCValues values;
  138.     XFontStruct *fontInfo;
  139.     char **fontList;
  140.     XColor exact, green;
  141.     unsigned long mask;
  142.     int count, i;
  143.  
  144.     glXWaitGL();
  145.  
  146.     fontList = XListFonts(dpy, pattern1, 1, &count);
  147.     if (count == 0) {
  148.         for (i = 0; i < 6; i++) {
  149.             pattern2[14] = size[i];
  150.             fontList = XListFonts(dpy, pattern2, 1, &count);
  151.             if (count > 0) {
  152.                 break;
  153.             }
  154.         }
  155.     }
  156.     if (count == 0) {
  157.        return GL_FALSE;
  158.     }
  159.  
  160.     fontInfo = XLoadQueryFont(dpy, fontList[0]);
  161.     if (fontInfo == NULL) {
  162.         return GL_FALSE;
  163.     }
  164.  
  165.     mask = GCForeground | GCFont;
  166.     values.font = fontInfo->fid;
  167.  
  168.     if (rgb) {
  169.         XAllocNamedColor(dpy, cmap, "Green", &exact, &green);
  170.         values.foreground = green.pixel;
  171.     } else {
  172.         values.foreground = 2;
  173.     }
  174.  
  175.     gc = XCreateGC(dpy, window, mask, &values);
  176.     XDrawString(dpy, window, gc, (int)x, (int)y, string, (int)len);
  177.  
  178.     XFreeFontNames(fontList);
  179.     glXWaitX();
  180.     return GL_TRUE;
  181. }
  182.  
  183. static void DoDisplay(void)
  184. {
  185.     glLoadIdentity();
  186.     glOrtho(0.0, W, 0.0, H, -0.5, 1000.0);
  187.  
  188.     if (swaps) {
  189.     glDrawBuffer(GL_BACK);
  190.     glClearColor(0.0, 0.0, 1.0, 0.0);
  191.     glClearIndex(2);
  192.     glClear(GL_COLOR_BUFFER_BIT);
  193.     glDrawBuffer(GL_FRONT);
  194.     glClearColor(0.0, 0.0, 0.0, 0.0);
  195.     glClearIndex(0);
  196.     glClear(GL_COLOR_BUFFER_BIT);
  197.     } else {
  198.     glDrawBuffer(GL_BACK);
  199.     glClearColor(0.0, 0.0, 0.0, 0.0);
  200.     glClearIndex(0);
  201.     glClear(GL_COLOR_BUFFER_BIT);
  202.     glDrawBuffer(GL_FRONT);
  203.     glClearColor(0.0, 0.0, 1.0, 0.0);
  204.     glClearIndex(2);
  205.     glClear(GL_COLOR_BUFFER_BIT);
  206.     }
  207.  
  208.     glDrawBuffer(drawBuffer);
  209.  
  210.     if (drawGL) {
  211.     glRasterPos2f(10.0, 10.0);
  212.     glCallLists(17, GL_UNSIGNED_BYTE, (unsigned char *)"this is a GL font");
  213.     glXWaitGL();
  214.     }
  215.     if (drawX) {
  216.     DrawXFont(10,40, "this is an X font", 17);
  217.     glXWaitX();
  218.     }
  219.  
  220.     if (drawBuffer == GL_FRONT)
  221.         printf("drawGL=%d, drawX=%d, drawbuffer=GL_FRONT\n",drawGL, drawX);
  222.     else
  223.         printf("drawGL=%d, drawX=%d, drawbuffer=GL_BACK\n",drawGL, drawX);
  224.     glFlush();
  225. }
  226.  
  227. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  228. {
  229.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  230.     return GL_TRUE;
  231.     }
  232.     return GL_FALSE;
  233. }
  234.  
  235. int main(long argc, char** argv)
  236. {
  237.     XVisualInfo *vi;
  238.     XSetWindowAttributes swa;
  239.     GLXContext cx;
  240.     XEvent event;
  241.     GLboolean needDisplay;
  242.     int i;
  243.     GLuint base;
  244.  
  245.     rgb = 1;
  246.     doubleBuf = 1;
  247.     for (i = 1; i < argc; i++) {
  248.         if (argv[i][0] == '-') {
  249.             switch (argv[i][1]) {
  250.               case 'c':
  251.                 rgb = 0;
  252.                 break;
  253.               default:
  254.                 Usage();
  255.             }
  256.         } else {
  257.             Usage();
  258.         }
  259.     }
  260.  
  261.     dpy = XOpenDisplay(0);
  262.     if (!dpy) {
  263.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  264.     return -1;
  265.     }
  266.  
  267.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  268.              (rgb ? RGBattributes_DB : CIattributes_DB));
  269.     if (!vi) {
  270.     fprintf(stderr, "No appropriate visual on \"%s\"\n",
  271.         getenv("DISPLAY"));
  272.     return -1;
  273.     }
  274.  
  275.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  276.                rgb ? AllocNone : AllocAll);
  277.     if (!rgb) {
  278.         XColor buf;
  279.         int i;
  280.  
  281.         buf.flags = DoRed | DoGreen | DoBlue;
  282.  
  283.         /* Init color map */
  284.         for (i=0; i<16; i++) {
  285.             buf.pixel = i;
  286.             buf.blue = (i & 4) ? 65535 : 0;
  287.             buf.green = (i & 2) ? 65535 : 0;
  288.             buf.red = (i & 1) ? 65535 : 0;
  289.             if (i > 8) {
  290.                 buf.red /= 2;
  291.                 buf.green /= 2;
  292.                 buf.blue /= 2;
  293.             }
  294.             XStoreColor(dpy, cmap, &buf);
  295.         }
  296.     }
  297.  
  298.  
  299.     swa.border_pixel = 0;
  300.     swa.colormap = cmap;
  301.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  302.     | KeyReleaseMask;
  303.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  304.                W, H,
  305.                0, vi->depth, InputOutput, vi->visual,
  306.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  307.     XSetWMColormapWindows(dpy, window, &window, 1);
  308.     XMapWindow(dpy, window);
  309.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  310.  
  311.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  312.     if (!glXMakeCurrent(dpy, window, cx)) {
  313.     fprintf(stderr, "Can't make window current to context\n");
  314.     return -1;
  315.     }
  316.  
  317. /****
  318.     if (!rgb) {
  319.     XColor xc[32+2];
  320.  
  321.     unsigned long pixel;
  322.     unsigned short red, green, blue;
  323.     char flags;  
  324.     char pad;
  325.  
  326.     for (i = 0; i < 16; i++) {
  327.         xc[i].pixel = i + COLOR_OFFSET_1;
  328.         xc[i].red = 0;
  329.         xc[i].green = 0;
  330.         xc[i].blue = (unsigned short) (65535.0 * (i / 15.0));
  331.         xc[i].flags = DoRed | DoGreen | DoBlue;
  332.  
  333.         xc[i+16].pixel = i + COLOR_OFFSET_2;
  334.         xc[i+16].red = 0;
  335.         xc[i+16].green = (unsigned short) (65535.0 * (i / 15.0));
  336.         xc[i+16].blue = 0;
  337.         xc[i+16].flags = DoRed | DoGreen | DoBlue;
  338.     }
  339.  
  340.     xc[32].pixel = BLUE;
  341.     xc[32].red = xc[32].green = 0;
  342.     xc[32].blue = 65535;
  343.     xc[32].flags = DoRed|DoGreen|DoBlue;
  344.  
  345.     xc[32].pixel = GREEN;
  346.     xc[32].red = xc[32].blue = 0;
  347.     xc[32].green = 65535;
  348.     xc[32].flags = DoRed|DoGreen|DoBlue;
  349.     XStoreColors(dpy, cmap, xc, 34);
  350.     xswap
  351. ****/
  352.  
  353.     base = LoadGLFont();
  354.     glListBase(base);
  355.  
  356.     needDisplay = GL_TRUE;
  357.     for (;;) {
  358.     do {
  359.         XNextEvent(dpy, &event);
  360.         switch (event.type) {
  361.           case Expose:
  362.         needDisplay = GL_TRUE;
  363.         break;
  364.           case ConfigureNotify:
  365.         W = event.xconfigure.width;
  366.         H = event.xconfigure.height;
  367.         needDisplay = GL_TRUE;
  368.         break;
  369.           case KeyPress:
  370.         {
  371.             char buf[100];
  372.             int rv;
  373.             KeySym ks;
  374.  
  375.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  376.             switch (ks) {
  377.               case XK_f:
  378.             drawBuffer = GL_FRONT;
  379.             needDisplay = GL_TRUE;
  380.             break;
  381.               case XK_b:
  382.             drawBuffer = GL_BACK;
  383.             needDisplay = GL_TRUE;
  384.             break;
  385.               case XK_g:
  386.             drawGL = !drawGL;
  387.             needDisplay = GL_TRUE;
  388.             break;
  389.               case XK_x:
  390.             drawX = !drawX;
  391.             needDisplay = GL_TRUE;
  392.             break;
  393.               case XK_r:
  394.             needDisplay = GL_TRUE;
  395.             break;
  396.               case XK_S:
  397.               case XK_s:
  398.             glXSwapBuffers(dpy, window);
  399.             swaps = !swaps;
  400.             break;
  401.               case XK_Escape:
  402.             return 0;
  403.             }
  404.         }
  405.         break;
  406.         }
  407.     } while (XPending(dpy) != 0);
  408.  
  409.     if (needDisplay) {
  410.         needDisplay = GL_FALSE;
  411.         DoDisplay();
  412.     }
  413.     }
  414. }
  415.